﻿using furion.blog.Domain.IRepositories;
using furion.blog.Domain.MySQL;
using furion.blog.Domain.Query.TagConfig;
using furion.blog.Domain.Shared;
using furion.blog.Domain.Shared.Vo.TagConfig;
using Furion.DependencyInjection;
using Furion.FriendlyException;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace furion.blog.EntityFramework.Core.Repositories
{
    public class TagConfigRepository : BaseSqlSugarRepository<TagConfig>, ITagConfigRepository, ISingleton
    {
        public readonly ISqlSugarClient _db;

        public TagConfigRepository(ISqlSugarClient db)
        {
            this._db = db;
        }

        /// <summary>
        /// 通过id查找
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<TagConfig> FindById(long id) {
            return await base.GetByIdAsync(id);
        }


        /// <summary>
        /// 查询第一个
        /// </summary>
        /// <param name="exp"></param>
        /// <returns></returns>
        public async Task<TagConfig> First(Expression<Func<TagConfig,bool>> exp) {
            return await base.GetFirstAsync(exp);
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="tagConfig"></param>
        /// <returns></returns>
        public async Task<bool> Save(TagConfig tagConfig) { 
            return await base.InsertAsync(tagConfig);
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="tagConfig"></param>
        /// <returns></returns>
        public async Task<bool> Update(TagConfig tagConfig) {
           return await base.UpdateAsync(tagConfig);
        }


        /// <summary>
        /// 标签分页
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public async Task<PageVO> List(TagConfigListQuery query) {
            try
            {
                var (strWhere, queryParams) = query.QueryParams();
                RefAsync<int> totalRecord = 0;
                string columns = string.Format(@"
t.tag_id as tagId, t.tag_label as tagLabel , t.tag_key as tagKey ,t.group_key as groupKey,
t.created_time as createdTime,t.is_delete as isDelete,t.status as status
");
                var list = await _db.SqlQueryable<object>
                    (@" select "+columns+" from `tag_config` t ")
                    .Where(strWhere)  
                    .AddParameters(queryParams)
                    .OrderBy(" t.createdTime desc ")
                    .ToPageListAsync(query.PageIndex, query.PageSize, totalRecord);
                return new PageVO { Records = list, Total = totalRecord };
            }
            catch (Exception ex) {
                throw Oops.Oh(ex.Message);
            }
        }


        /// <summary>
        /// 查询左右
        /// </summary>
        /// <param name="exp"></param>
        /// <returns></returns>
        public async Task<List<TagConfig>> All(Expression<Func<TagConfig, bool>> exp) {
            return await base.GetListAsync(exp);
        }

    }
}
